package com.etooling.framework.controller;

import com.etooling.framework.entity.FileResource;
import com.etooling.framework.repository.FileResourceRepository;
import com.etooling.framework.svo.ReplyObj;
import com.etooling.framework.utils.VoToPoUtil;
import com.etooling.framework.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@Controller
@EnableAutoConfiguration
@RequestMapping(value = "/admin/fileSource")
/**
 * @title: DataDirController
 * @description:
 * @author 周志荣
 * @date 2020/12/19 12:11
 */
public class FileSourceController {
    @Autowired
    FileResourceRepository fileResourceRepository;

    @RequestMapping("pageQuery")
    @ResponseBody
    /**
     　　 @description:
    　　 @param [request, FileResource, pageSize, page, isUnion, searchName] 
    　　 @return com.irobot.framework.svo.ReplyObj
    　　 @throws
    　　 @author 周志荣
    　　 @date 2020/12/20 18:25 
    　　*/
    ReplyObj pageQuery(HttpServletRequest request,

                       @RequestParam(defaultValue = "30", required = false, value = "pageSize") int pageSize,
                       @RequestParam(defaultValue = "1", required = false, value = "page") int page,
                       @RequestParam(defaultValue = "yes", required = false, value = "searchValue") String searchValue

    ) {
        Sort sort = Sort.by(Sort.Direction.ASC, "id");//使用一个序
        //分页
        Pageable pageable = PageRequest.of(page - 1, pageSize, sort);
        Specification<FileResource> spec = new Specification<FileResource>() {
            @Override
            public Predicate toPredicate(Root<FileResource> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//                cb.between(root.<Date>get("from"),new Date(),new Date());//第一个对象属性在后面两个参数之间
                //cb.between(new Date(), root.<Date>get("from"), root.<Date>get("to"));//第一个转入值在后面两个对象内之间
                List<Predicate> predicates = new ArrayList<>();
                //将条件根据变量放进条件列表中

                //返回条件
                return WebUtils.getPredicates(predicates, null, cb);
            }
        };
        Page<FileResource> pageList = fileResourceRepository.findAll(spec, pageable);
        return WebUtils.flushSuccess(pageList);

    }


    @RequestMapping("add")
    @ResponseBody
    /**
     　　 @description:
    　　 @param [request, FileResource]
    　　 @return com.irobot.framework.svo.ReplyObj
    　　 @throws
    　　 @author 周志荣
    　　 @date 2020/12/19 14:13
    　　*/
    ReplyObj add(HttpServletRequest request, FileResource FileResource) {
        fileResourceRepository.saveAndFlush(FileResource);
        return WebUtils.flushObjSuccess(FileResource);
    }

    @RequestMapping("update")
    @ResponseBody
        /*
         * @description: 修改机构
         * @param [request, org, newOrgId]
         * @return com.irobot.framework.svo.ReplyObj
         * @throws
         * @author 周志荣
         * @date 2020/12/18 18:13
         */
    ReplyObj update(HttpServletRequest request, FileResource vo) {
        try {
            FileResource po = fileResourceRepository.findById(vo.getId()).get();
            VoToPoUtil.copyProperties(po, vo);
            fileResourceRepository.saveAndFlush(po);
            return WebUtils.flushObjSuccess(po);
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("数据错误，请联系统管理员");
        }

    }


    @RequestMapping("applResource")
    @ResponseBody
    ReplyObj applResource(HttpServletRequest request, @RequestParam(defaultValue = "", required = false, value = "id") String id) {
        try {

            FileResource po = fileResourceRepository.findById(id).get();
            fileResourceRepository.clearResource(po.getType());
            po.setStatus("应用中");
            fileResourceRepository.saveAndFlush(po);
            return WebUtils.flushObjSuccess(po);
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure("数据错误，请联系统管理员");
        }

    }

    @RequestMapping("del")
    @ResponseBody
        /*
         * @description: 删除机构
         * @param [request, org, newOrgId]
         * @return com.irobot.framework.svo.ReplyObj
         * @throws
         * @author 周志荣
         * @date 2020/12/18 18:14
         */
    ReplyObj del(HttpServletRequest
                         request, @RequestParam(defaultValue = "", required = false, value = "id") String id) {
        try {
            FileResource FileResource = fileResourceRepository.findById(id).get();
            fileResourceRepository.delete(FileResource);
            return WebUtils.flushSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return WebUtils.flushFailure();
        }

    }


}
